package com.example.demo.tools.pianoTools;

import org.springframework.ui.ModelMap;

import java.util.*;

public class piano {
	/*list:
	 * [52,53,52,53] [52,53,52,51] [52,53,54,53] [45,53,54] [52,53,52,53] [45,53,45] [52,53,52,53] [45,53,45] [52,53,52,53] [45,53,45] [52,53,54,53]
	 * 
	 * 
	 * 
	 * 
	 */
    static final String right[] = {"53","52"};
    static final String left[] = {"31","35","41","35"};
    static ArrayList<int[]> as = r();
    public static ArrayList<int[]> r() {
    	ArrayList<int[]> as = new ArrayList<>();
    	int a1[] = {52,53,52,53};
    	int a2[] = {52,53,52,51};
    	int a3[] = {52,53,54,53};
    	int a4[] = {0,45,53,54};
    	int a5[] = {52,53,52,53};
    	int a6[] = {0,45,53,45};
    	int a7[] = {52,53,52,53};
    	int a8[] = {0,45,53,45};
    	int a9[] = {52,53,52,53};
    	int a10[] = {0,45,53,45};
    	int a11[] = {52,53,52,53};
        for (int i = 0; i < 3; i++) {
            as.add(a1);
        }
    	as.add(a2);as.add(a3);as.add(a4);as.add(a5);as.add(a6);as.add(a7);as.add(a8);as.add(a9);as.add(a10);as.add(a11);
    	return as;
    }
    public static ArrayList<ModelMap> r2(){
		 ArrayList<ModelMap> sum = new ArrayList<>();
    	 int time = 0;
    	for (int[] is : as) {
			for (int i = 0; i < is.length; i++) {
				ModelMap model = new ModelMap();
				model.addAttribute("time", time);
                model.addAttribute("key", is[i]);
                sum.add(model);
                time += 300;
			}
		}
    	return sum;
    }
    
    
    public static ModelMap song(){
        PianoKV pianoKV = new PianoKV();
        Map<Double, Integer> kv = pianoKV.getPianoKV();

        ArrayList<ModelMap> RSong = new ArrayList<>();
        ArrayList<ModelMap> LSong = new ArrayList<>();
        ArrayList<String> AllKeys = new ArrayList<>();
        for (int i = 0; i < 4 ; i++) {
            REight(RSong, i);
        }
        LEight(RSong,LSong);
        for (int i = 0; i < RSong.size(); i++) {
            String key = (String)RSong.get(i).get("key");
            AllKeys.add(key);
        }
        for (int i = 0; i < LSong.size(); i++) {
            String key = (String)LSong.get(i).get("key");
            AllKeys.add(key);
        }
        Set<String> set = new HashSet<String>();
        set.addAll(AllKeys);
        AllKeys.clear();
        AllKeys.addAll(set);

        ModelMap model = new ModelMap();
        model.addAttribute("LSong", LSong);
        model.addAttribute("RSong", RSong);
        model.addAttribute("AllKeys", AllKeys);
        return model;
    }

    public static List<ModelMap> LEight(ArrayList<ModelMap> RSong, ArrayList<ModelMap> LSong){
        int index = 0;
        for (int i = 16; i < RSong.size(); i++) {
            if(((i&1) != 1)){
                ModelMap model = new ModelMap();
                model.addAttribute("time", RSong.get(i).get("time"));
                model.addAttribute("key", left[index]);
                LSong.add(model);
                if(index == left.length - 1){
                    index = 0;
                }else{
                    index = index + 1;
                }
            }else{
                continue;
            }
        }
        return LSong;
    }

    public static List<ModelMap> REight(ArrayList<ModelMap> song, int index){
        for (int i = index * 8; i < (index+1) * 8; i++) {
            ModelMap model = new ModelMap();
            int time = 300 * i;
            String key = ((i&1) != 1)? right[1] : right[0];
            model.addAttribute("time", time);
            model.addAttribute("key", key);
            song.add(model);
        }
        return song;
    }
}
